package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ObjectNotFoundException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;

import java.util.List;
import java.util.Scanner;

/**
 * @author 黄衡熙
 * @version 1.0
 */

public class StartController {

    private Scanner sc = new Scanner(System.in);
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");

    public static void main(String[] args) {
        new StartController().showMenu();
    }

    public void showMenu() {
        System.out.println("**************************欢迎来到紫金商城**************************");
        System.out.print("请选择服务(1.注册 2.登录 3.查看商品信息 4.分页查询 5.退出):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findProducts();
                break;
            case 4:
                findPage();
                break;
            case 5:
                System.out.println("提示:您已经退出紫金商城");
                break;
            default:
                System.out.println("错误:对不起,您输入的选项有误,请重新输入!");
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入手机号码:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();

        try {
            // 调用业务层方法实现注册功能
            userService.register(username, password, phone, address);
            System.out.println("提示:恭喜您,注册成功!");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
        }
        showMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        try {
            // 调用业务层方法实现登录功能
            User user = userService.login(username, password);
            // 登录成功之后进入主页,必须将当前登录的用户信息传递进去
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:登录失败，" + e.getMessage());
            showMenu();
        } catch (ObjectNotFoundException e) {
            System.out.println("错误:登录失败，" + e.getMessage());
            showMenu();
        }
        showMenu();
    }

    public void findProducts() {
        List<Product> products = productService.findAll();
        System.out.println("*********************商品列表*********************");
        System.out.println("ID\t商品名称\t价格");
        for (Product product : products) {
            String output = product.getId() + "\t"
                    + product.getName() + "\t"
                    + product.getPrice();
            System.out.println(output);
        }
        showMenu();
    }
    public void findPage() {
        System.out.print("请输入查询的页码:");
        String pageNo = sc.next();
        System.out.print("请输入当前页的商品数量：");
        String pageSize = sc.next();
        for (Product product : productService.findPage(pageNo, pageSize)) {
            String output = product.getId() + "\t"
                    + product.getName() + "\t"
                    + product.getPrice();
            System.out.println(output);
        }
        showMenu();
    }
}
